package cn.snorlaxzjt.LBSserver.common;


import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;


/**
 * 向前端返回信息封装
 * @param <T> 可变类型
 */
@ApiModel(description = "rest请求的返回模型，所有rest正常都返回该类的对象")
@Getter
@NoArgsConstructor
public class Result<T> {

    @ApiModelProperty(value = "code", required = true)
    private Integer code;
    @ApiModelProperty(value = "处理结果描述信息")
    private String msg;
    @ApiModelProperty(value = "处理结果数据信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    public Result(BusinessCode errorType) {
        this.code = errorType.getCode();
        this.msg = errorType.getMsg();
    }

    public Result(BusinessCode errorType, T data) {
        this(errorType);
        this.data = data;
    }

    private Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public boolean isSuccess(){
        return this.code.equals(BusinessCode.SUCCESS.getCode());
    }
    /**
     * 未登录返回结果
     */
    public static <T> Result<T> unauthorized(T data) {
        return new Result<T>(BusinessCode.UNAUTHORIZED.getCode(), BusinessCode.UNAUTHORIZED.getMsg(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> Result<T> forbidden(T data) {
        return new Result<T>(BusinessCode.FORBIDDEN.getCode(), BusinessCode.FORBIDDEN.getMsg(), data);
    }
    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }





    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
